import datetime
import json
import requests
from Config.Config import *


# 读取文件
def read(path):
     with open(path, "rb") as f:
        data = f.read().decode()
        f.close()
     return data

# 读取文件并转为字典
def read_json(path):
    with open(path, "rb") as f:
        data = f.read().decode()
        data = json.loads(data)
        f.close()
    return data

# 配置文件地址
def config_path():
    #windows路径
    #config_path = prj_path + "\\Config\\config.json"
    #linux路径
    config_path = prj_path + "/Config/config.json"
    return config_path

# 读取配置文件
def read_config():
    config_data = read_json(config_path())
    return config_data

# 获取access_token
def get_access_token(url,data):
    req = requests.post(url, data).json()
    access_token = req["data"]["access_token"]
    return access_token

# 拼装herders
def headers():
    headers = read_config()["headers"]
    login_url = read_config()["login_url"]
    access_token = get_access_token(login_url, read_config()["user"])
    headers["Authorization"] = "Bearer " + str(access_token)
    return headers

# 获取H5_access_token
def get_H5_access_token(url,data,headers):
    req = requests.post(url, data=json.dumps(data),headers=headers).json()
    access_token = req["data"]["access_token"]
    return access_token

# 拼装H5-herders
def H5_headers():
    headers = read_config()["headers"]
    login_H5_url = read_config()["login_H5_url"]
    access_token = get_H5_access_token(login_H5_url, read_config()["H5_user"], headers)
    headers["Authorization"] = "Bearer " + str(access_token)
    return headers


